import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class ReadFile {
	
	private String filePath = "";
	private UIMain uiMain = null;
	private int line = -1;
	private long sec = -1;
	private String find = "";
	
	public ReadFile (UIMain uiMain) {
		this.uiMain = uiMain;
	}
	
	public void setFilePath(String path) {
		this.filePath = path;
	}
	
	public boolean setLine(String val) {
		try {
			this.line = Integer.parseInt(val);
		} catch (NumberFormatException e) {
			if (val.trim().equals("")) {
				line = -1;
				return true;
			}
			else
				return false;
		}
		return true;
	}
	
	public boolean setSec (String val) {
		try {
			this.sec = Long.parseLong(val);	
		} catch (NumberFormatException e) {
			if (val.trim().equals("")) {
				sec = -1;
				return true;
			}
			else
				return false;
		}
		return true;
	}
	
	public void setFind(String find) {
		this.find = find;
	}
	
	public boolean read() {
		if (filePath.isEmpty()) {
			return false;
		}
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(filePath));

			String s; 
			int idx=1;
			long currentTime = System.currentTimeMillis();
	
			while ((s = in.readLine()) != null) {
				
				long during = System.currentTimeMillis() - currentTime;
				if (during >=  (sec*1000) && sec != -1)
					break;
				
				if (s.indexOf(find)==-1 && !find.isEmpty())
					continue;
				
				uiMain.appendResult("[" + idx++ + "]-------------------------------------------------------------------------------------------------------------------------------");
				uiMain.appendResult(s);
				
				if ((idx-1) >= line && line != -1)
					break;
					
			}

			in.close();
		} catch (IOException e) {
			System.err.println(e); 
			System.exit(1);
		} finally {
			uiMain.appendResult("********************* End of Result *********************");
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		
	}
}
